package com.eattravel.controller;

import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.Home;
import com.eattravel.pojo.Store;
import com.eattravel.service.HomeHotStoreService;
import com.eattravel.service.HomeService;
import com.eattravel.vo.HomeHotFood;
import com.eattravel.service.HomeHotFoodService;
import com.eattravel.vo.HomeHotStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author glong
 * @date 2019/9/19 11:14
 * 描述：主页的操作
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private HomeHotFoodService homeHotFoodService;

    @Autowired
    private HomeHotStoreService homeHotStoreService;

    @Autowired
    private HomeService homeService;

    /**
     * 热门美食部分内容
     * @return
     */
    @RequestMapping("/hotfood")
    @ResponseBody
    public Map<String ,Object> getHotFood(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<HomeHotFood> hotFoodsList = new ArrayList<>();
        // 取出查询到的数据
        List<Commodity> list = null;
        try {
            list = homeHotFoodService.getHomeHotFoodList();
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                HomeHotFood food = new HomeHotFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                hotFoodsList.add(food);
            }
            modelMap.put("hotFoodList", hotFoodsList);
            modelMap.put("total",hotFoodsList.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("/test")
    @ResponseBody
    public Map<String ,Object> test(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<HomeHotFood> hotFoodsList = new ArrayList<>();
        // 取出查询到的数据
        List<Commodity> list = null;
        try {
            list = homeHotFoodService.getHomeHotFoodList();
            modelMap.put("hotFoodList", list);
            modelMap.put("total",list.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    /**
     * 热门商铺部分内容
     * @return
     */
    @RequestMapping("/hotstore")
    @ResponseBody
    public Map<String ,Object> getHomeHotStore(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<HomeHotStore> hotStoreList = new ArrayList<>();
        // 取出查询到的数据
        List<Store> list = null;
        try {
            list = homeHotStoreService.getHotStore();
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                HomeHotStore store = new HomeHotStore();
                store.setStoreId(list.get(i).getStoreId());
                store.setStoreName(list.get(i).getStoreName());
                store.setStoreDescribe(list.get(i).getStoreDescribe());
                store.setPicture(list.get(i).getPictureList().get(0).getPicture());
                hotStoreList.add(store);
            }
            modelMap.put("hotStoreList", hotStoreList);
            modelMap.put("total",hotStoreList.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    /**
     * 广告部分内容
     * @return
     */
    @RequestMapping("/advertise")
    @ResponseBody
    public Map<String ,Object> getAdvertise(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        // 取出查询到的数据
        List<Home> homes = null;
        try {
            // 获取查询的全部广告数据
            homes = homeService.selectAll();
            modelMap.put("advertiseList", homes);
            modelMap.put("total",homes.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",false);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

}
